<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入创建人"></p-input>
        </p-form-item>
        <p-form-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择创建日期" v-decorator="[ 'createTime', validatorRules.createTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'updateBy', validatorRules.updateBy]" placeholder="请输入更新人"></p-input>
        </p-form-item>
        <p-form-item label="更新日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择更新日期" v-decorator="[ 'updateTime', validatorRules.updateTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择父级节点"
            v-decorator="['pid', validatorRules.pid]"
            dict="material_category,aad,id"
            pidField="pid"
            pidValue="0"
            hasChildField="has_child">
          </j-tree-select>
        </p-form-item>
        <p-form-item label="物料类别码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'classCode', validatorRules.classCode]" placeholder="请输入物料类别码"></p-input>
        </p-form-item>
        <p-form-item label="物料类别名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'className', validatorRules.className]" placeholder="请输入物料类别名称"></p-input>
        </p-form-item>
        <p-form-item label="物料类别说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'comment', validatorRules.comment]" placeholder="请输入物料类别说明"></p-input>
        </p-form-item>
        <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择时间标识" v-decorator="[ 'timestamp', validatorRules.timestamp]" :trigger-change="true" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
        </p-form-item>
        <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
        </p-form-item>
        <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
        </p-form-item>
        <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
        </p-form-item>
        <p-form-item label="简单描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'aad', validatorRules.aad]" placeholder="请输入简单描述"></p-input>
        </p-form-item>
        <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
        </p-form-item>

      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'

  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JTreeSelect from '@/components/jeecg/JTreeSelect'
  
  export default {
    name: "MaterialCategoryModal",
    components: { 
      JDate,
      JTreeSelect
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          createBy: {rules: [
          ]},
          createTime: {rules: [
          ]},
          updateBy: {rules: [
          ]},
          updateTime: {rules: [
          ]},
          pid: {rules: [
          ]},
          classCode: {rules: [
          ]},
          className: {rules: [
          ]},
          comment: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
          aad: {rules: [
          ]},
          remark: {rules: [
          ]},
        },
        url: {
          add: '/basic-business/basicMaterialCategory/materialCategory/add',
          edit: '/basic-business/basicMaterialCategory/materialCategory/edit',
        },
        expandedRowKeys:[],
        pidField: 'pid'
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','pid','classCode','className','comment','timestamp','companyId','factoryId','clientId','substituteId','aad','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let old_pid = this.model[this.pidField]
            let formData = Object.assign(this.model, values);
            let new_pid = this.model[this.pidField]
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.submitSuccess(formData,old_pid==new_pid)
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','pid','classCode','className','comment','timestamp','companyId','factoryId','clientId','substituteId','aad','remark'))
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }
      
      
    }
  }
</script>